package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ApphomeModuleItemBinding;
import sz.szsmk.citizencard.model.network.bean.response.QueryMainPageFunctionRes;
import sz.szsmk.citizencard.util.ACache;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/20 14:30
 * description:
 */
public class ApphomeModuleItemAdapter extends RecyclerView.Adapter<ApphomeModuleItemAdapter.ApphomeModuleItemHolder> {

    private Context mContext;
    private List<QueryMainPageFunctionRes.MainPageClassListBean.MainPageGroupListBean.MainPageFunctionListBean> viewData;

    public ApphomeModuleItemAdapter(Context context) {
        mContext = context;
    }


    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header, String modularId);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.apphome_module_item;
    }

    @Override
    public ApphomeModuleItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_module_item, parent, false);
        return ApphomeModuleItemHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(ApphomeModuleItemHolder holder, int position) {
        loadItemData(holder.binding, position);
    }

    private void loadItemData(ApphomeModuleItemBinding binding, final int position) {
        if (viewData != null) {

            String json = new Gson().toJson(viewData);


            binding.apphomeModuleItemTv1.setText(viewData.get(position).getName());
            binding.apphomeModuleItemTv2.setText(viewData.get(position).getSub_title());
            Glide.with(mContext)
                    .load(viewData.get(position).getIcon_url())
                    .into(binding.apphomeModuleItemIv);
            final QueryMainPageFunctionRes.MainPageClassListBean.MainPageGroupListBean.MainPageFunctionListBean bean = viewData.get(position);
            String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
            binding.goverItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = bean.getLink_url();
                    String sys_label = bean.getSys_label();
                    int need_header = bean.getNeed_header();
                    String modularId = bean.getModularId();
                    if (url == null) {
                        return;
                    }

                    if (baseInterface!=null){
                        baseInterface.onClick(v,url,sys_label,need_header,modularId);
                    }

                }
            });

//            binding.gover_item
//            QueryMainPageFunctionRes.MainPageClassListBean.MainPageGroupListBean.MainPageFunctionListBean bean = viewData.get(position);
//            binding.goverItem
        }
    }

    @Override
    public int getItemCount() {
        return null == viewData ? 0 : viewData.size();
    }

    public void setViewData(List<QueryMainPageFunctionRes.MainPageClassListBean.MainPageGroupListBean.MainPageFunctionListBean> viewData) {
        this.viewData = viewData;
    }


    public static class ApphomeModuleItemHolder extends RecyclerView.ViewHolder {
        private ApphomeModuleItemBinding binding;

        public static ApphomeModuleItemHolder creat(ViewGroup parent) {
            ApphomeModuleItemBinding binding =
                    ApphomeModuleItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new ApphomeModuleItemHolder(binding);
        }

        public ApphomeModuleItemHolder(ApphomeModuleItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
